Skip to content

refactor(search-query): generic normalizeQuery pipeline for pre-parse query repair (CLI-FA)#880

Merged
BYK merged 3 commits intomainfrom
fix/cli-fa-generic-query-repair
Apr 29, 2026
Merged

refactor(search-query): generic normalizeQuery pipeline for pre-parse query repair (CLI-FA)#880
BYK merged 3 commits intomainfrom
fix/cli-fa-generic-query-repair

Conversation

@BYK
Copy link
Copy Markdown
Member

@BYK BYK commented Apr 29, 2026

Summary

Follow-up to #872 — replaces the specific tryRepairQuery (single regex, catch-block-only) with a generic normalizeQuery pipeline that runs before PEG parsing on every query.

What changed

Before (PR #872)

  • tryRepairQuery only ran when PEG parsing failed (catch block)
  • Single regex for [a,b,)[a,b]
  • Couldn't fix patterns that PEG parses but the API rejects (e.g., [a,b,])

After (this PR)

  • normalizeQuery runs on every query before PEG parsing — cheap string ops
  • Pipeline of focused transform functions:
    1. fixMismatchedBrackets: [a,b,)[a,b] (wrong closing delimiter)
    2. stripTrailingListCommas: [a,b,][a,b] (balanced bracket trailing comma)
    3. Whitespace collapse: double spaces → single, trim edges
  • Architecture makes it easy to add more passes without growing complexity

Why pre-parse?

Only 1 of the observed CLI-FA patterns actually fails PEG parsing — the rest parse fine but the Sentry API rejects them semantically. Running normalization before parsing catches both syntactic and PEG-valid-but-API-invalid patterns.

Tests

  • 62 tests: unit tests for each normalizer (brackets, commas, whitespace, passthrough, cross-boundary safety)
  • Integration tests verifying the full sanitizeQuerynormalizeQuery → PEG parse flow
  • Property tests still pass

…Query pipeline (CLI-FA)

Replaces the specific tryRepairQuery (catch-block-only, single regex) with
a normalizeQuery pipeline that runs BEFORE PEG parsing on every query:

1. fixMismatchedBrackets: [a,b,) → [a,b] (wrong closing delimiter)
2. stripTrailingListCommas: [a,b,] → [a,b] (trailing comma in balanced brackets)
3. Whitespace collapse: double spaces → single, trim edges

The pipeline architecture makes it easy to add more normalization passes
(e.g., date format repair, quote balancing) without growing complexity.

Pre-parse normalization is cheaper and more predictable than the previous
approach of only repairing in the PEG failure catch block.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 29, 2026

PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://cli.sentry.dev/_preview/pr-880/

Built to branch gh-pages at 2026-04-29 04:40 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 29, 2026

Codecov Results 📊

6290 passed | Total: 6290 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +1
Passed Tests 📈 +1
Failed Tests
Skipped Tests

All tests are passing successfully.

❌ Patch coverage is 60.00%. Project has 13110 uncovered lines.
✅ Project coverage is 75.91%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
src/lib/search-query.ts 60.00% ⚠️ 16 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    75.87%    75.91%    +0.04%
==========================================
  Files          294       294         —
  Lines        54454     54412       -42
  Branches         0         0         —
==========================================
+ Hits         41312     41302       -10
- Misses       13142     13110       -32
- Partials         0         0         —

Generated by Codecov Action

Comment thread src/lib/search-query.ts Outdated
Comment thread src/lib/search-query.ts Outdated
…collapse

1. MALFORMED_IN_LIST_RE now only matches ) as wrong closing delimiter,
   not ] (which is handled by stripTrailingListCommas). Removes the
   overlap between the two pipeline steps.

2. Removes whitespace collapse — it would alter content inside quoted
   values like message:"connection  timed out".

Addresses Cursor Bugbot medium + low severity findings.
Comment thread src/lib/search-query.ts Outdated
Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b55cfb0. Configure here.

Comment thread src/lib/search-query.ts
@BYK BYK merged commit 9950b47 into main Apr 29, 2026
26 checks passed
@BYK BYK deleted the fix/cli-fa-generic-query-repair branch April 29, 2026 04:51
BYK added a commit that referenced this pull request Apr 29, 2026
…ing quoted values (#881)

## Summary
Fixes a bug from PR #880 where `normalizeQuery` could corrupt quoted
string content. Since the normalization pipeline runs on **every** query
(not just PEG failures), bracket repairs like `[500,]` → `[500]` would
also fire inside quoted values like `message:"error [500,] found"`.

## Fix
Introduces `transformUnquoted()` — splits the query at double-quoted
boundaries (respecting escaped quotes `\"`) and only applies
normalization passes to unquoted segments. Quoted regions are preserved
verbatim.

## Changes
- **`src/lib/search-query.ts`**: Add `QUOTED_SEGMENT_RE`,
`transformUnquoted()`, wrap `normalizeQuery` pipeline inside
`transformUnquoted`
- **`test/lib/search-query.test.ts`**: 10 new tests covering quote
preservation, mixed quoted/unquoted repairs, escaped quotes, and
`transformUnquoted` unit tests

## Examples
| Input | Before this fix | After this fix |
|---|---|---|
| `message:"error [500,] found"` | `message:"error [500] found"`
(corrupted) | `message:"error [500,] found"` (preserved) |
| `level:[error,) message:"[trailing,]"` | both brackets fixed | only
`level` fixed, quote preserved |

Addresses
#880 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant